/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren;

import io.netty.util.HashedWheelTimer;
import io.renren.config.BizConfig;
import io.renren.config.TXYCosConfig;
import io.renren.modules.oss.cloud.COSCloudStorageService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
//@EnableDiscoveryClient
//@EnableApolloConfig
public class RenrenApplication {

    public static void main(String[] args) {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
        SpringApplication.run(RenrenApplication.class, args);
    }


    //region netty 时间轮

    /**
     * 时间轮
     *
     * @return
     */
    @Bean(BizConfig.NETTY_TIME_WHEEL)
    public HashedWheelTimer getTimerWheel() {
        //设置 100ms 的内部轮询间隔(默认)
        return new HashedWheelTimer(Executors.defaultThreadFactory(), 100, TimeUnit.MILLISECONDS, 2, true, 100);
    }

    //endregion

    //region 腾讯云

    @Bean(BizConfig.COS_CLOUD)
    public COSCloudStorageService cosService(TXYCosConfig config){
        return new COSCloudStorageService(config.getCConfig());
    }

    //endregion

    //region 配置 SSL （算了还是在 Nginx 配置吧，做一层隔离，还可以防止代码入侵）

    /**
     * http重定向到https
     * @return
     */
//    @Bean
//    public ServletWebServerFactory servletContainer() {
//        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
//            @Override
//            protected void postProcessContext(Context context) {
//                SecurityConstraint securityConstraint = new SecurityConstraint();
//                securityConstraint.setUserConstraint("CONFIDENTIAL");
//                SecurityCollection collection = new SecurityCollection();
//                collection.addPattern("/*");
//                securityConstraint.addCollection(collection);
//                context.addConstraint(securityConstraint);
//            }
//        };
//        tomcat.addAdditionalTomcatConnectors(httpConnector());
//        return tomcat;
//    }
//
//    @Bean
//    public Connector httpConnector() {
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//        connector.setScheme("http");
//        //Connector监听的http的端口号
//        connector.setPort(8080);
//        connector.setSecure(false);
//        //监听到http的端口号后转向到的https的端口号
//        connector.setRedirectPort(80);
//        return connector;
//    }

    //endregion

}